﻿// Copyright (c) Vitaly Kamiansky. All rights reserved. 
// See License.txt in the project root.

using Property.Reloaders;
using System;

namespace Property
{
    /// <summary>
    /// Contains words that can come immediately after the opening
    /// of a property description.
    /// </summary>
    public static class ReloadableExtensionsCore
    {
        /// <summary>
        /// ... synchronously (each time) ...
        /// </summary>
        public static IReloaderPrototype<T> Each<T>(this Reloadable<T> source)
        {
            if (source == null) throw new ArgumentNullException("source");

            return new SyncReloaderPrototype<T>();
        }

        /// <summary>
        /// ... synchronously if the specified condition is met (when) ...
        /// </summary>
        /// <param name="condition">Tell me how to decide if the reload should happen.</param>
        public static IReloaderPrototype<T> Conditional<T>(this Reloadable<T> source, Func<bool> condition)
        {
            return source.Each().Conditional(condition);
        }

        /// <summary>
        /// ... synchronously if the discriminator state meets the specified condition (when) ...
        /// </summary>
        /// <param name="getState">Tell me how the state should be produced 
        /// based on its previous value.</param>
        /// <param name="condition">Tell me how to use the state to decide if 
        /// the reload should happen.</param>
        public static IReloaderPrototype<T> Conditional<T, TState>(this Reloadable<T> source, Func<TState, TState> getState, Func<TState, bool> condition)
        {
            return source.Each().Conditional(getState, condition);
        }

        /// <summary>
        /// ... synchronously first time ...
        /// </summary>
        public static IReloaderPrototype<T> First<T>(this Reloadable<T> source)
        {
            return source.Each().First();
        }

        /// <summary>
        /// ... synchronously every odd time ...
        /// </summary>
        public static IReloaderPrototype<T> Odd<T>(this Reloadable<T> source)
        {
            return source.Each().Odd();
        }
    }
}
